package com.grapevine.contact.local;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;
import android.text.TextUtils;

public class NumberLocalProvider extends ContentProvider implements BaseColumns
{
	private static final String AUTHORITIES = "com.grapevine.contact.contentprovider";

	private static final String DATABASE_NAME = "numberlocal.db";
	private static final String TABLE_NAME = "phonelocal";

	public static final String NUMBER = "number";
	public static final String PROVINCE = "province"; // 省
	public static final String CITY = "city"; // 市
	public static final String AREA_CODE = "area_code"; // 区号
	public static final String POST_CODE = "post_code"; // 邮编
	public static final String TELCOM = "tel_com"; // 移动或者联通
	public static final String NETWORK_TYPE = "net_stype";// 网络类型 GSM 或者 CDMA

	private static final String CREATE_TABLE = "create table phonelocal(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ NUMBER + " TEXT," + PROVINCE + " TEXT," + CITY + " TEXT," + AREA_CODE + " TEXT," + POST_CODE + " TEXT,"
			+ TELCOM + " TEXT," + NETWORK_TYPE + " TEXT" + ");";

	private static final int DATABASE_VERSION = 1;
	private static final UriMatcher matcher;

	public static final int SEARCH = 0;
	public static final int SEARCH_ITEM = 1;

	private DataBaseHelp mDataBaseHelp;
	public static final Uri CONNECT_URI = Uri.parse("content://" + AUTHORITIES + "/" + TABLE_NAME);

	static
	{
		matcher = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(AUTHORITIES, TABLE_NAME, SEARCH);
		matcher.addURI(AUTHORITIES, TABLE_NAME + "/#", SEARCH_ITEM);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs)
	{
		int count = 0;
		SQLiteDatabase db = mDataBaseHelp.getWritableDatabase();
		switch (matcher.match(uri))
		{
		case SEARCH:
			db.delete(TABLE_NAME, selection, selectionArgs);
			break;
		case SEARCH_ITEM:
			String id = uri.getPathSegments().get(1);
			count = db.delete(TABLE_NAME, _ID + "=" + id, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("not find uri = " + uri);
		}
		return count;
	}

	@Override
	public String getType(Uri uri)
	{
		switch (matcher.match(uri))
		{
		case SEARCH:
			return SearchManager.SUGGEST_MIME_TYPE;
		case SEARCH_ITEM:
			return SearchManager.SHORTCUT_MIME_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URL " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values)
	{
		SQLiteDatabase db = mDataBaseHelp.getWritableDatabase();

		long row = 0;
		if (matcher.match(uri) != SEARCH)
		{
			throw new IllegalArgumentException("error uri " + uri);
		}

		row = db.insert(TABLE_NAME, null, values);

		if (row > 0)
		{
			Uri noteUri = ContentUris.withAppendedId(CONNECT_URI, row);
			getContext().getContentResolver().notifyChange(uri, null);
			return noteUri;
		}
		return null;
	}

	@Override
	public boolean onCreate()
	{
		mDataBaseHelp = new DataBaseHelp(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
	{
		SQLiteDatabase db = mDataBaseHelp.getReadableDatabase();
		Cursor cursor = null;

		switch (matcher.match(uri))
		{
		case SEARCH:
			cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
			break;
		case SEARCH_ITEM:
			String id = uri.getPathSegments().get(1);
			cursor = db.query(TABLE_NAME, projection, _ID + "=" + id
					+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs, null, null,
					sortOrder);
			break;

		default:
			throw new IllegalArgumentException("error uri " + uri);
		}
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
	{
		SQLiteDatabase db = mDataBaseHelp.getWritableDatabase();
		int count = 0;

		switch (matcher.match(uri))
		{
		case SEARCH:
			db.update(TABLE_NAME, values, selection, selectionArgs);
			break;
		case SEARCH_ITEM:
			String id = uri.getPathSegments().get(1);
			count = db.update(TABLE_NAME, values, _ID + "=" + id
					+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("error uri " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	private static final class DataBaseHelp extends SQLiteOpenHelper
	{
		public DataBaseHelp(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db)
		{
			db.execSQL(CREATE_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}
}
